﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
using Newtonsoft.Json.Linq;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public string getTradeContribs(string portfolioId, string nodedates)
        {
            Product product = new Product();

            product.Name = "Apple";

            product.Expiry = new DateTime(2008, 12, 28);

            product.Price = 3.99M;

            product.Sizes = new string[] { "Small", "Medium", "Large" };



            string json = JsonConvert.SerializeObject(product);
            //string json = JsonConvert.SerializeObject(new Home().getData(nodedates));

            return json;

        }

        public Object insert(string key, string value) {
            new Home().insert(key, value);
            return "ok";
        }

        public Object getData() {

            System.Net.WebRequest request = System.Net.WebRequest.Create("http://localhost:7631/Home/getTradeContribs?portfolioId=1&nodedates=2");

            request.Method = "POST";
            request.ContentType = "application/json; charset=utf-8";
            Product product;
            JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            //get response-stream, and use a streamReader to read the content
            using (Stream s = request.GetResponse().GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(s))
                {
                    //Test test = (Test)json_serializer.DeserializeObject(); 
                    //decode jsonData with javascript serializer

                    //JObject jObject = JObject.Parse(sr.ReadToEnd());
                    //JToken jUser = jObject["Name"] + "<br/>" + jObject["Expiry"] + "<br/>" + jObject["Price"] + "<br/>" + jObject["Sizes"];
                    return sr.ReadToEnd();
                    //Product p = new Product(sr.ReadToEnd();

                    foreach (var item in p.Sizes)
                    {
                        return item;
                    }
                    return p.Name;


                }
            }

        }

    }
    public class User
    {
        public User(string json)
        {
            JObject jObject = JObject.Parse(json);
            JToken jUser = jObject["user"];
            name = (string)jUser["name"];
            teamname = (string)jUser["teamname"];
            email = (string)jUser["email"];
            players = jUser["players"].ToArray();
        }

        public string name { get; set; }
        public string teamname { get; set; }
        public string email { get; set; }
        public Array players { get; set; }
    }
}
